<?php
class RegisterController extends AppController {
	var $name = 'Register';
	var $components = array('Menu','Init','Captcha');
	var $uses = array();

	function beforeFilter() {
		$this->Init->startup($this);
		$this->login = $this->Init->init();
		$this->Init->getKeyword();
		$_session = $this -> Session -> read();
		$this->loadModel('Hitcounter');
		$row = $this->Hitcounter->get(session_id());
		if ($row == 0) {
			$data = array(
				"session_id" => session_id(),
				"ref_url" => env("REQUEST_URI"),
				"ip_address" => env("REMOTE_ADDR"),
				"activity" => date("Y-m-d H:i:s")
			);
			$this->Hitcounter->save($data);
		}
		$this->set('activeNav', '');
	}

	function index() {
		$this->layout = 'inside';

		$this->set('captcha_image_url', Router::url(array('controller'=>'captcha', 'action'=>'securimage'))); //url for the captcha image

                $captcha_error_msg = __('The code you entered does not match', true);

		if(!empty($this->data)) {
                        if( $this->captcha->check($this->data['Security']['code']) == false ) {
				$this->Session->setFlash(__('The security code was incorrect.', true),'alert');
                                $this->redirect(array('controller' => 'register','action' => 'index'));
			}
			$this->loadModel('Member');
			$this->data['Member']['createDate'] = date('Y/m/d H:i');
			$this->data['Member']['passwd'] = Security::hash($this->data['Member']['ipasswd'], null, true);
			$this->data['Member']['token'] = md5(time()  +  rand());
			$this->Member->set( $this->data );
			if ($this->Member->validates()) {
				if ($this->Member->save($this->data)) {
						$this->Session->setFlash(__('Acount created. You can login.', true));
						$this->redirect(array('controller'=>'login', 'action'=>'index'));
				}
			} else {
				$errors = $this->Member->invalidFields();
				$this->Session->setFlash(implode('<br/>', $errors),'alert');
			}

		}

		$this->render('index');
	}
}
?>
